package com.brp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brp.po.Cargo;
import com.brp.po.Removecargo;
import com.brp.po.Sale;
import com.brp.dao.SaleMapper;
import com.brp.service.ICargoService;
import com.brp.service.IRemovecargoService;
import com.brp.service.ISaleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brp.service.ISalerecordService;
import com.brp.vo.ResultVo;
import com.brp.vo.SaleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author all
 * @since 2022-03-16
 */
@Service
public class SaleServiceImpl extends ServiceImpl<SaleMapper, Sale> implements ISaleService {
    @Autowired
    private ICargoService cargoService;
    @Autowired
    private SaleMapper saleMapper;
    @Autowired
    private IRemovecargoService removecargoService;
    @Autowired
    private ISalerecordService salerecordService;

    @Override
    public ResultVo<Cargo> getAllCargo(Integer pageNo, Integer pageSize) {
        Page<Cargo> page = new Page<>(pageNo,pageSize);
        IPage<Cargo> iPage = cargoService.selectPage(page);
        int count = (int) iPage.getTotal();
        List<Cargo> cargos = iPage.getRecords();
        ResultVo<Cargo> resultVo = new ResultVo(count,cargos);
        return resultVo;
    }

    @Override
    public ResultVo<Cargo> getOneCargo(String cargoName, String cargoType) {
        QueryWrapper<Cargo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("cargo_name",cargoName);
        queryWrapper.like("cargo_type",cargoType);
        Cargo one = cargoService.getOne(queryWrapper);
        List<Cargo> list = Arrays.asList(new Cargo[]{one});
        ResultVo<Cargo> resultVo = new ResultVo(10,list);
        return resultVo;
    }

    @Override
    @Transactional
    public boolean addRemoveCargo(Integer cargoId) {
        Removecargo removecargo = new Removecargo();
        removecargo.setFkCargoId(cargoId);
        boolean save = removecargoService.save(removecargo);
        return save;
    }

    @Override
    @Transactional
    public boolean deleteRemoveCargo(Integer id) {
        boolean b = removecargoService.removeById(id);
        return b;
    }

    @Override
    @Transactional
    public boolean deleteRemoveCargoAll(SaleVo saleVo) {
        List<Removecargo> list = removecargoService.list();
        Integer integer = saleMapper.insertSale(saleVo);
        if (integer > 0){
            for (Removecargo p:list) {
                Cargo byId = cargoService.getById(p.getFkCargoId());
                QueryWrapper<Cargo> queryWrapper = new QueryWrapper<>();
                Integer cargoRepertory = byId.getCargoRepertory()-p.getCargoNumber();
                saleVo.setCargoId(p.getFkCargoId());
                if (cargoRepertory >= 0){
                    queryWrapper.eq("cargo_repertory",cargoRepertory);
                    if (!salerecordService.insertSaleRecord(saleVo)){
                        return false;
                    }
                }else {
                    return false;
                }
            }
        }
        boolean b = removecargoService.removeRemoveCargoAll();
        return b;
    }

    @Override
    @Transactional
    public boolean updateNumber(Integer number) {
        QueryWrapper<Removecargo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cargo_number",number);
        boolean update = removecargoService.update(queryWrapper);
        return update;
    }
}
